Skip to content

fix: async cache storing exception fixed #548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

akmalviya03
Copy link

Earlier async cache was caching exceptions.
With new changes in fetch method of AsyncCache class will no longer store exception.
By default AsyncCache will store exception, by setting _canCacheException to false. It won't store exception.

Please refer #366 for video demo of both issue and solution along with minimal reproducible code.


  • I’ve reviewed the contributor guide and applied the relevant portions to this PR.

@mosuem mosuem requested a review from a team as a code owner April 17, 2025 13:00
AsyncCache(Duration duration) : _duration = duration;
/// If [cacheErrors] is `false` the cache will be invalidated if the [Future]
/// returned by the callback completes as an error.
AsyncCache(Duration duration, {bool cacheErrors = true})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any naming we can use that would make the flag default to false?
Just because it's usually a good idea to have false as the default value.

Maybe bool ephemeralErrors = false. (Ouch, would hate to have to write that).
flushErrors?


/// Creates a cache that invalidates after an in-flight request is complete.
///
/// An ephemeral cache guarantees that a callback function will only be
/// executed at most once concurrently. This is useful for requests for which
/// data is updated frequently but stale data is acceptable.
AsyncCache.ephemeral() : _duration = null;
AsyncCache.ephemeral(): _duration = null, _cacheErrors = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The _cacheErrors here feels like it should be false (since it doesn't cache errors, because it doesn't cache anything). It's invisible to the user, so it doesn't really matter, and I guess it just means "don't special-case errors".
It's OK, just a little confusing.

I guess there are really three modes:

  • Ephemeral everything.
  • Ephemeral errors, persistent values.
  • Persistent everything.

covered by two constructors. Should we just have three constructors?

var errorThrowingFuture = cache.fetch(asyncFunctionThatThrows);
await expectLater(errorThrowingFuture, throwsA(isException));

var valueFuture = cache.fetch(() async => 'Success');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider checking that time hasn't progressed by one hour. Just for good measure.

@@ -18,6 +18,20 @@ void main() {
cache = AsyncCache(const Duration(hours: 1));
});

test('should not fetch when callback throws exception', () async {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure "fetch" is the verb I'd use here (aka: I don't know what the description means).
It's consistent with the other tests. I also can't read those.

That's not really your issue to fix, though. (Should we say "recompute" instead of "fetch". Why is the method called fetch to begin with, seems to assume a specific use-case.)

Copy link

PR Health

Breaking changes ⚠️
Package Change Current Version New Version Needed Version Looking good?
async Non-Breaking 2.13.0 2.13.1-wip 2.14.0
Got "2.13.1-wip" expected >= "2.14.0" (non-breaking changes)
⚠️

This check can be disabled by tagging the PR with skip-breaking-check.

Changelog Entry ✔️
Package Changed Files

Changes to files need to be accounted for in their respective changelogs.

Coverage ⚠️
File Coverage
pkgs/async/lib/src/async_cache.dart 💔 87 % ⬇️ 6 %

This check for test coverage is informational (issues shown here will not fail the PR).

This check can be disabled by tagging the PR with skip-coverage-check.

API leaks ✔️

The following packages contain symbols visible in the public API, but not exported by the library. Export these symbols or remove them from your publicly visible API.

Package Leaked API symbols
License Headers ✔️
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
Files
no missing headers

All source files should start with a license header.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants